package com.uranus.common.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uranus.common.VO.ServiceContext;
import com.uranus.common.common.beans.Dict;
import com.uranus.common.common.mapper.DictMapper;
import com.uranus.common.common.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uranus.common.constant.enums.LanguageEnum;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ybx
 * @since 2018-12-22
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public List<Dict> getDictList(String dict) {
        Map<String,Object> map=new HashMap<>();
        map.put("dict",dict);
        List<Dict> list=this.baseMapper.selectByMap(map);
        if(list==null){
            return new ArrayList<Dict>();
        }
        return list;
    }
    @Override
    public List<Dict> getDictListByDicts(String dicts) {
       String [] dictArray= dicts.split(",");
       if(dictArray==null || dictArray.length==0){
           return new ArrayList<Dict>();
       }
       List<String> listIds=Arrays.asList(dictArray);
        List<Dict> list= this.baseMapper.selectBatchIds(listIds);
        if(list==null){
            return new ArrayList<Dict>();
        }
        return list;
    }
    @Override
    public String getDictName(List<Dict> list, Integer value) {
        for (Dict item:list){
            if(item.getDictValue()==value){
                if(ServiceContext.getInstance().getLanguageEnum()== LanguageEnum.zh_cn){
                    return item.getNameZhCn();
                }
                if(ServiceContext.getInstance().getLanguageEnum()== LanguageEnum.en_us){
                    return item.getNameEnUs();
                }
            }
        }
        return "";
    }
    @Override
    public String getDictName(List<Dict> list, String dict, Integer value) {
        for (Dict item:list){
            if(item.getDictValue()==value && item.getDict().toLowerCase().equals(dict.toLowerCase())){
                if(ServiceContext.getInstance().getLanguageEnum()== LanguageEnum.zh_cn){
                    return item.getNameZhCn();
                }
                if(ServiceContext.getInstance().getLanguageEnum()== LanguageEnum.en_us){
                    return item.getNameEnUs();
                }
            }
        }
        return "";
    }
    @Override
    public String getDictName(String dict, Integer value) {
        List<Dict> list= getDictList(dict);
        return getDictName(list,value);
    }
}
